function Get-NewCredential {
    Param(
        [Parameter(Mandatory = $false)][string]$Message,
        [Parameter(Mandatory = $false)][string]$UserName,
        [Parameter(Mandatory = $false)][switch]$Confirm,
        [Parameter(Mandatory = $false)][string[]]$Blocklist,
        [Parameter(Mandatory = $false)][string]$Regex
    )

    do {
            # Ask for username and password.
            if(!($Username)){
                $cred = Get-Credential -Message $Message  
            }
            else {
                $cred = Get-Credential -Message $Message -UserName $UserName
            }
            $pass = ConvertTo-LSPlainText -SecureString $cred.Password
            
            if ($Blocklist.Count -gt 0) 
            {
                if ($cred.UserName -iin $Blocklist) 
                {
                    Write-Warning "The user name '$($cred.UserName)' is forbidden. Please use another user name."
                    $cred = $null
                    $UserName = $null
                    continue
                }
                elseif ($pass.ToLower().Contains($cred.Username.ToLower())) 
                {
                    # Passwords contains username.
                    Write-Warning 'Password contains Azure SQL server user name! Please re-enter the user name and select a new password.'
                    $cred = $null
                    $UserName = $null
                    continue
                }
                elseif ($pass -cnotmatch $Regex) 
                {
                    # Password does not fit regular expression.
                    Write-Warning 'Password does not comply to the complexity rules stated above. Please re-enter the user name and select a new password.'
                    $cred = $null
                    $UserName = $null
                    continue
                }
            }
            
            if ($Confirm) 
            {
                # Ask user to retype his password.
                $confirmCreds = Get-Credential -UserName $cred.UserName -Message 'Please confirm your password'

                # Check if passwords match.
                if ([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.Password)) -cne [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($confirmCreds.Password))) 
                { 
                    # Passwords don't match.
                    Write-Warning 'Passwords did not match! Try again.'
                    $cred = $null
                }
            }
        } while (!$cred)
    return $cred

    <#
    .SYNOPSIS
        Get-Credential (With optional password confirmation)
    .DESCRIPTION
        Asks for a user name and password.
        Possible to ask user to confirm his password by retyping it.
    .EXAMPLE
        PS C:\> Get-NewCredential
        
        Windows PowerShell credential request.
        Create new username and password.
        User: <user>
        Password for user <user>: ********

        UserName                     Password
        --------                     --------
        <user>   System.Security.SecureString
    .EXAMPLE
        PS C:\> Get-NewCredential -Message "Hello, I need your user name and password."
        
        Windows PowerShell credential request.
        Hello, I need your user name and password.
        User: <user>
        Password for user <user>: ********

        UserName                     Password
        --------                     --------
        <user>   System.Security.SecureString
    .EXAMPLE
        PS C:\> Get-NewCredential -Message "Hello, I need your user name and password." -UserName "bob"
        
        Windows PowerShell credential request.
        Hello, I need your user name and password.
        Password for user bob: ********

        UserName                     Password
        --------                     --------
        bob      System.Security.SecureString
    .EXAMPLE
        PS C:\> Get-NewCredential -Confirm
        
        Windows PowerShell credential request.
        Create new username and password.
        User: <user>
        Password for user <user>: ********
        Please confirm your password
        Password for user <user>: ********

        UserName                     Password
        --------                     --------
        <user>   System.Security.SecureString
    .INPUTS
        None. You cannot pipe objects to Get-NewCredential.
    .OUTPUTS
        PSCredential.
    .PARAMETER Message
        A string that is displayed before asking for the credentials.
        Required?                    false
        Position?                    
        Default value                Create new username and password.
        Accept pipeline input?       false
        Accept wildcard characters?  false
    .PARAMETER UserName
        The user name property of the PSCredential.
        Required?                    false
        Position?                    
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
    .PARAMETER Confirm
        A switch parameter used if user should have to retype his password for confirmation.
        Required?                    false
        Position?                    
        Default value                
        Accept pipeline input?       false
        Accept wildcard characters?  false
    #>
}